package cs342.uwosh.edu.ContactsApp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cs342.uwosh.edu.ContactsApp.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

public class SearchActivity extends Activity {
    /** Called when the activity is first created. */
	private ListView listView;
	private ContactsDataSource db;
	private List<Contact> contacts;
	private Contact[] contactArray;
	static String searchString;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayAdapter<Contact> arrayAdapter;// = new ArrayAdapter();
        listView =  (ListView)findViewById(R.id.listView1);
        
        //List<Contact> contacts = new ArrayList<Contact>();
        db = new ContactsDataSource(this);
        db.open();
        contacts = db.getAllContacts();
        db.close();
        searchString = searchString.toLowerCase();
        //System.out.println(searchString + " here");
        for(int i = 0; i< contacts.size(); i++) {
        	System.out.println(contacts.get(i));
        	if(!contacts.get(i).getName().toLowerCase().contains(searchString)) {
        		contacts.remove(i);
        		i--;
        	}
        }
        if(contacts.size() == 0) {
        	AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("None Found");
            alert.setMessage("No contacts matched search");

            // Set an EditText view to get user input 
    		//final EditText input = new EditText(this);
    		//alert.setView(input);

    		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int whichButton) {
    				//SearchActivity.searchString = input.getText().toString();
    				
    				Intent i = new Intent(SearchActivity.this, ContactsActivity.class);
    		    	startActivity(i);
    				
    			}
    		});
    		
    		alert.show();
        } else {
        
        contactArray = new Contact[1];
        contactArray = contacts.toArray(contactArray);
        
        Arrays.sort(contactArray);
        
        
        arrayAdapter = new ArrayAdapter<Contact>(this, R.layout.contact_list, contactArray);
        
        listView.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view,
        	        int position, long id) {
        		Contact contact = contactArray[(int) id];
        		DisplayContact.id=contact.getID();
        		Intent i = new Intent(SearchActivity.this, DisplayContact.class);
        		SearchActivity.this.startActivity(i);
        	}
        });
        
        
        listView.setAdapter(arrayAdapter);
        }
        
    }
    
    
    public void add(View view) {
    	Intent i = new Intent(this, AddContact.class);
    	startActivity(i);
    }
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    		case R.id.addOptionsMenu:
    			this.add(getCurrentFocus());
    	}
    	return true;
    }
}